package com.test.map.HTTP;

import android.view.View;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class LoadJson {
    public void loadjson(final View view) {
        new Thread(){
            @Override
            public void run() {
                try {
                    URL Url=new URL("http://192.168.199.239:8080/2.json");
                    HttpURLConnection conn=(HttpURLConnection)Url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setReadTimeout(5000);
                    int code = conn.getResponseCode();
                    if (code==200){
                        InputStream inputStream = conn.getInputStream();
                        String s = ToString(inputStream);
                        jiexi(s);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }



    private void jiexi(String s) throws JSONException {
        JSONObject object=new JSONObject(s);
        System.out.println(s);
        String name = object.getString("name");
        String gender = object.getString("gender");
        String age = object.getString("age");
        String hobby = object.getString("hobby");
        final String[] strings = {name, gender, age, hobby};

    }


    private String ToString(InputStream in) throws IOException {
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int len=-1;
        while ((len=in.read(bytes))!=-1){
            baos.write(bytes,0,len);
        }

        in.close();
        String s=new String(baos.toByteArray());
        return s;
    }
}
